package com.niiwoo.civet.trade.service.dubbo.project;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.activity.stage.service.notice.NoviceTaskRecordDubboService;
import com.niiwoo.civet.trade.dao.entity.ProjectInvestOrder;
import com.niiwoo.civet.trade.dao.mapper.NewbieProjectConfigMapperExt;
import com.niiwoo.civet.trade.dto.response.IsShowNoviceTaskEntranceResponseDTO;
import com.niiwoo.civet.trade.service.local.invest.InvestOrderService;
import com.niiwoo.civet.trade.service.project.NovicePorjectDubboService;
import com.niiwoo.civet.trade.utils.RepaymentUtil;
import com.niiwoo.civet.user.dto.response.UserBasicInfoResponseDTO;
import com.niiwoo.civet.user.enums.QuestionIdentityTagEnum;
import com.niiwoo.civet.user.enums.UserEnum;
import com.niiwoo.civet.user.service.UserBasicInfoDubboService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;

@Slf4j
@Service(version = "1.0.0")
public class NovicePorjectDubboServiceImpl implements NovicePorjectDubboService {

    @Reference(version = "1.0.0")
    private UserBasicInfoDubboService userBasicInfoDubboService;

    @Autowired
    private NewbieProjectConfigMapperExt newbieProjectConfigMapperExt;

    @Autowired
    private InvestOrderService investOrderService;

    @Value("${online_time_for_NoviceTask}")
    String onlineTime;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Reference(version ="1.0.0")
    private NoviceTaskRecordDubboService noviceTaskRecordDubboService;

    /**
     * 新手定义：
     *
     * @param userId
     * @return ((byte) 0, "新手"),((byte) 1, "投资人"),((byte) 2, "借款人");
     */
    @Override
    public Byte queryUserIdentity(String userId) {
        UserBasicInfoResponseDTO userBasicInfo = userBasicInfoDubboService.selectUserBaseInfo(userId);
        if (userBasicInfo == null) {
            return null;
        }
        /*NewbieProjectConfig newbieProjectConfig = newbieProjectConfigMapperExt.selectNewbieProjectConfig(null);
        Integer days = RepaymentUtil.getOverdueDay(userBasicInfo.getCreateTime(), new Date());
        //注册天数在 配置的天数中，都算新手
        if (newbieProjectConfig != null && days < newbieProjectConfig.getRoleDefinition()) {
            return QuestionIdentityTagEnum.NEW_HAND.getValue();
        } else {
            return queryUserIdentity(userBasicInfo).getValue();
        }*/
        return queryUserIdentity(userBasicInfo).getValue();
    }

    public QuestionIdentityTagEnum queryUserIdentity(UserBasicInfoResponseDTO userBasicInfo) {
        String identityTagStr = userBasicInfo.getIdentityTag();
        if (UserEnum.IdentityTag.INVEST_BORROWER.getType().equals(identityTagStr)) {
            return QuestionIdentityTagEnum.INVESTOR;
        } else if (UserEnum.IdentityTag.INVESTOR.getType().equals(identityTagStr)) {
            return QuestionIdentityTagEnum.INVESTOR;
        } else if (UserEnum.IdentityTag.BORROWER.getType().equals(identityTagStr)) {
            return QuestionIdentityTagEnum.BORROWER;
        } else {
            return QuestionIdentityTagEnum.NEW_HAND;
        }
    }

    public IsShowNoviceTaskEntranceResponseDTO isNoviceTaskEntrance(String userId) {
        IsShowNoviceTaskEntranceResponseDTO dto = new IsShowNoviceTaskEntranceResponseDTO();
        UserBasicInfoResponseDTO userBasicInfo = userBasicInfoDubboService.selectUserBaseInfo(userId);
        if(userBasicInfo == null){
            return dto;
        }
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd");
        DateTime sDateTime = format.parseDateTime(onlineTime);
        Date date = sDateTime.toDate();
        Integer day = RepaymentUtil.getOverdueDay(date,userBasicInfo.getCreateTime());
        //在该版本上线后注册的新用户（可以只是注册手机号码），登录状态下
        if (day >= 0) {
            QuestionIdentityTagEnum questionIdentityTagEnum = queryUserIdentity(userBasicInfo);
            //未投资过
            if (QuestionIdentityTagEnum.NEW_HAND.getValue().equals(questionIdentityTagEnum.getValue())) {
                dto.setIsShowNoviceTaskEntrance((byte) 1);
            } else if (QuestionIdentityTagEnum.INVESTOR.getValue().equals(questionIdentityTagEnum.getValue())) {
                //首投30天内
                ProjectInvestOrder projectInvestOrder = investOrderService.selectFristInvestProject(userId);
                if (projectInvestOrder == null) {
                    dto.setIsShowNoviceTaskEntrance((byte) 1);
                } else {
                    Integer investDay = RepaymentUtil.getOverdueDay(projectInvestOrder.getCreateTime(), new Date());
                    if (investDay <= 30) {
                        dto.setIsShowNoviceTaskEntrance((byte) 1);
                    }
                }
            } else {
                dto.setIsShowNoviceTaskEntrance((byte) 1);
            }
        }
        dto.setDay(day);
        return dto;
    }

}
